package cn.itsource.pet.service.impl;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.dto.OrderPetAcquisitionConfirmDto;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhm
 * @since 2023-04-06
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {


    @Autowired
    private PetMapper petMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    @Transactional
    public AjaxResult confirm(OrderPetAcquisitionConfirmDto dto) {
        Long id = dto.getId();
        Integer payType = dto.getPayType();
        BigDecimal money = dto.getMoney();
        //更新收狗单
        OrderPetAcquisition order = super.queryById(id);
        order.setPaytype(payType);
        order.setPrice(money);
        order.setState(1);
        super.update(order);
        //更新宠物确认价格
        Pet pet = petMapper.queryById(order.getPetId());
        pet.setCostprice(money.multiply(new BigDecimal("0.8")));
        pet.setSaleprice(money.multiply(new BigDecimal("1.5")));
        petMapper.update(pet);
        //更新寻主消息状态 已完成
        SearchMasterMsg msg = searchMasterMsgMapper.queryById(order.getSearchMasterMsgId());
        msg.setState(3);
        searchMasterMsgMapper.update(msg);
        return AjaxResult.me();
    }
}
